package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.constant.enums.OrderStatusEnum;
import com.tbit.uqbike.object.pojo.ParkPoint;
import com.tbit.uqbike.object.pojo.ReturnBikeLog;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.dto.CommonDTO;
import com.tbit.uqbike.service.business.ReturnBikeLogService;
import com.tbit.uqbike.webmanager.dao.core.ParkPointDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.log.ReturnBikeLogDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author HZW
 * @ClassName ReturnBikeLogServiceImpl
 * @Description
 * @date 2021/12/27 14:50
 * @Version 1.0
 */
@Service("returnBikeLogService")
public class ReturnBikeLogServiceImpl implements ReturnBikeLogService {
    @Resource
    private ReturnBikeLogDao returnBikeLogDao;
    @Resource
    private UserDao userDao;
    @Resource
    private ParkPointDao parkPointDao;

    @Override
    public List<ReturnBikeLog> selectInfo(CommonDTO commonDTO) {

        Map<String, Object> map = new HashMap<>();
        Map<Integer, User> userMap = new HashMap<>();
        Map<Integer, ParkPoint> parkPointMap = new HashMap<>();
        List<ReturnBikeLog> list = returnBikeLogDao.selectInfo(commonDTO);
        List<Integer> userIds = list.stream().map(ReturnBikeLog::getUserId).collect(Collectors.toList());
        if (!userIds.isEmpty()) {
            map.put("userIds", userIds);
            List<User> userList = userDao.getByUserIds(map);
            if (!userList.isEmpty()) {
                for (User user : userList) {
                    userMap.put(user.getUserId(), user);
                }
            }
        }
        Set<Integer> parkPointIds = list.stream().map(ReturnBikeLog::getParkPointId).collect(Collectors.toSet());
        if (!parkPointIds.isEmpty()) {
            List<ParkPoint> parkPoints = parkPointDao.getParkpointByIds(parkPointIds);
            if (!parkPoints.isEmpty()) {
                for (ParkPoint parkPoint : parkPoints) {
                    parkPointMap.put(parkPoint.getParkPointId(), parkPoint);
                }
            }
        }
        for (ReturnBikeLog returnBikeLog : list) {
            /**将错误码替换成错误信息*/
            OrderStatusEnum statusEnum = OrderStatusEnum.of(returnBikeLog.getReturnCode());
            if (statusEnum != null) {
                returnBikeLog.setReturnMessage(statusEnum.getDesc());
            }
            User user = userMap.get(returnBikeLog.getUserId());
            if (user == null) {
                continue;
            }
            ParkPoint parkPoint = parkPointMap.get(returnBikeLog.getParkPointId());
            if (returnBikeLog.getUserId().equals(user.getUserId()) && null != user.getPhone()) {
                returnBikeLog.setPhone(user.getPhone());
            }
            /**填充站点名*/
            if (parkPoint != null && null != returnBikeLog.getParkPointId() && null != parkPoint.getParkPointId() && null != parkPoint.getName()) {
                if (returnBikeLog.getParkPointId().equals(parkPoint.getParkPointId())) {
                    returnBikeLog.setParkPointName(parkPoint.getName());
                }
            }
        }
        return list;
    }
}
